package com.lwl.blog.core.exception;

import com.lwl.blog.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * blog项目全局异常管理器
 * 2类异常，业务异常，系统异常
 * </p>
 *
 * @author lwl
 * @since 22-12-26
 */

@RestControllerAdvice
@Slf4j
public class BlogExceptionHanlder {

    //业务异常
    @ExceptionHandler(BaseException.class)
    public R baseException(BaseException e) {

        log.error(e.getMsg());
        return R.error(e.getMsg());
    }

    //系统异常
    @ExceptionHandler(Exception.class)
    public R systemException(Exception e){

        log.error("系统异常！");
        return R.error("系统正在维护,请稍后再试!");
    }

    // 出现JsonException或者它的子类调用这个方法
    @ExceptionHandler(JsonException.class)
    @ResponseBody
    public R jsonException(JsonException e) {
        log.error("出现json异常", e);
        return R.error(e.getMsg());
    }

    //view异常调用这个方法的条件了
    @ExceptionHandler(ViewException.class)
    public ModelAndView viewException(ViewException e) {
        log.error("出现了view异常", e);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("error.html");
        mv.addObject("msg",e.getMsg());
        return mv;
    }

}
